<?php

class Domain_traffic extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function get($cond) {
		if (!$cond['domain_id']) return array();
		$domain_id = implode(',', $cond['domain_id']);
		$q = "SELECT sum(traffic) as s, updated_at FROM traffic WHERE domain_id IN ({$domain_id}) AND updated_at BETWEEN '{$cond['start_date']}' AND '{$cond['end_date']}' GROUP BY updated_at";
		$query = $this->db->query($q);
		$raw_data = $query->result_array();
		$r = array();
		$r['total'] = 0;
		$r['peak'] = 0;
		$r['peak_time'] = '';
		foreach ($raw_data as $v) {
			$b = $v['s']/(1024*8*300);
			$r['total'] += $b;
			$r['data'][] = array(strtotime($v['updated_at'])*1000,  $b);
			if ($r['peak'] < $b) {
				$r['peak'] = $b;
				$r['peak_time'] = $v['updated_at'];
			}
		}
		return $r;
	}

}
